package com.qf.controller.business;

import com.qf.entitys.Business;
import com.qf.entitys.MerchantClassification;
import com.qf.entitys.UrbanAreas;
import com.qf.service.BusinessService;
import com.qf.service.MerchantClassificationService;
import com.qf.service.UrbanAreasService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * @Author:ztl
 * @Date:2023/01/30
 * @内容：
 */
@Controller
@RequestMapping("/business")
public class BRegisterController {
    @Resource
    private MerchantClassificationService service;

    @Resource
    private UrbanAreasService urbanAreasService;

    @Resource
    private BusinessService businessService;

    @GetMapping("/register")
    public String register(HttpServletRequest request){
        List<MerchantClassification> merchantClassifications = service.selectAll();
        request.setAttribute("mcs",merchantClassifications);
        List<UrbanAreas> urbanAreas = urbanAreasService.selectByAdCode();
        request.setAttribute("province",urbanAreas);
        return "business/register";
    }

    @PostMapping("/register")
    public String register(Business business, @RequestParam(name = "bimgs") MultipartFile[] file1, @RequestParam(name = "qts") MultipartFile[] file2, HttpServletRequest request, RedirectAttributes model){
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        StringBuilder bimgs=new StringBuilder();
        StringBuilder qts=new StringBuilder();
        String realPath1 = request.getServletContext().getRealPath("/statics/business/images");
        String realPath2 = request.getServletContext().getRealPath("/statics/business/resource");
        for (MultipartFile f : file1){
            String extension = FilenameUtils.getExtension(f.getOriginalFilename());
            String newFileName=System.currentTimeMillis()+new Random().nextInt(100000)+"."+extension;
            bimgs.append(basePath+"/statics/business/images/"+newFileName).append(",");
            File newFile=new File(realPath1,newFileName);
            try {
                f.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        business.setBimg(bimgs.substring(0,bimgs.length()-1));

        for (MultipartFile f : file2){
            String extension = FilenameUtils.getExtension(f.getOriginalFilename());
            String newFileName=System.currentTimeMillis()+new Random().nextInt(100000)+"."+extension;
            qts.append(basePath+"/statics/business/resource/"+newFileName).append(",");
            File newFile=new File(realPath2,newFileName);
            try {
                f.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        business.setQualifications(qts.substring(0,qts.length()-1));
        business.setBactive(false);

        int i = businessService.insertSelective(business);
        if(i>0){
            return "redirect:/login";
        }else {
            model.addFlashAttribute("syserror","系统升级，请一会儿再试!");
            return "redirect:/error";
        }
    }
}
